/**
 * @overview 添加 WMTS
 * @author dengqiang
 * @description 引用鲸云 CgsTileLayer 类
 * @update  2022.04.06
 */
class GTileLayer {
  constructor() {
    window.GTileLayerArray = []
  }

  add(paramObj) {
    const { name, parentName, option } = paramObj
    const optionModified = {
      url: option.url,
      layer: option.layer,
      text: option.text,
      level: option.level,
      maxVisLevel: option.maxVisLevel,
      minVisLevel: option.minVisLevel,
      visible: option.visible || true,
      isAddToLayerTree: option.isAddToLayerTree || true,
      tileMatrixLabels: option.tileMatrixLabels,
      pId: option.pId,
      type: option.type,
      proxy: option.proxy || false,
    }
    const tileLayer = new CgsTileLayer(optionModified)
    const tileLayerObject = {
      name: name,
      parentName: parentName,
      tileLayer: tileLayer,
    }
    window.map.addLayer(tileLayer)
    window.GTileLayerArray.push(tileLayerObject)
  }

  //根据name移除单个围墙
  removeByName(name) {
    window.GTileLayerArray.forEach((item, index) => {
      if (item.name == name) {
        window.map.removeLayer(item.tileLayer)
        window.GTileLayerArray.splice(index, 1)
      }
    })
  }

  //根据parentName移除一批围墙
  removeByPatName(parentName) {
    const length = window.GTileLayerArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GTileLayerArray[i].parentName == parentName) {
        window.map.removeLayer(window.GTileLayerArray[i].tileLayer)
        window.GTileLayerArray.splice(i, 1)
      }
    }
  }

  //移除所有围墙
  removeAll() {
    window.GTileLayerArray.forEach((item) => {
      window.map.removeLayer(item.tileLayer)
    })
    window.GTileLayerArray = []
  }

  //根据name控制单个围墙的显隐
  setVisibleByName(name, boolean) {
    window.GTileLayerArray.forEach((item) => {
      if (item.name == name) {
        item.tileLayer.setVisible(boolean)
      }
    })
  }

  //根据parentName控制一批围墙的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GTileLayerArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.tileLayer.setVisible(boolean)
      }
    })
  }
}

export default GTileLayer
